package jsu.blogger;
import jakarta.annotation.Resource;
import jsu.blogger.config.RedisKeyPrefixConfig;
import jsu.blogger.mapper.PostMapper;
import jsu.blogger.pojo.Post;
import jsu.blogger.service.inter.PostService;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@MapperScan("jsu.blogger.mapper")
public class testBlog {
    @Resource
    private PostMapper postMapper;
    @Test
    void testGetPostById(){
        //测试
        System.out.println(postMapper.getBlogDetailApartContentById(1L));
    }

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void saveUserInterestTag(Long userId, String tagName, Integer weight, Long timestamp) {
        String key = RedisKeyPrefixConfig.getUserInterestTagsKey(userId);
        // 创建一个 Map 用于存储标签的详细信息
        Map<String, Object> tagDetails = new HashMap<>();
        tagDetails.put("weight", weight);
        tagDetails.put("timestamp", timestamp);

        // 将标签信息存储到 Redis 的哈希中
        redisTemplate.opsForHash().put(key, tagName, tagDetails);
    }
    @Test
    void setRedisInterestTag(){
        //System.out.println(RedisKeyPrefixConfig.getUserInterestTagsKey(1L));
        Long userId = 1L; // 示例用户 ID
        String tagName = "vue"; // 标签名称
        Integer weight = 4; // 权重
        Long timestamp = System.currentTimeMillis(); // 当前时间戳
        // 存储用户的兴趣标签
        saveUserInterestTag(userId, tagName, weight, timestamp);
        // System.out.println(postMapper.selectPostById(1L));
        // Post post= postMapper.selectPostById(1L);
        //System.out.println(post);

    }
    @Autowired
    private PostService postService;
    @Test
    void testGetPostTitleAndContentById(){
        Post post=postService.getPostTitleAndContentById(1L,1L,0);
        System.out.println(post);
    }


    @Test
    void testInsertPost(){
        Post post = new Post();
//        private Long postId;  // 帖子的唯一标识
//        private Long userId;  // 发布帖子的用户ID
//        private String title;  // 帖子标题
//        private String content="";  // 帖子内容
//        private Integer postType;  // 帖子类型，0表示博客，1表示动态
//        private Timestamp createdAt;  // 创建时间
//        private Timestamp updatedAt;  // 更新时间
        post.setUserId(1L);
        post.setTitle("你好");
       // post.setCreatedAt();
        post.setPostType(0);
        post.setContent("jjj");
// 设置 post 的其他字段，如 userId, title, content 等
        postMapper.insertPost(post);
        System.out.println("生成的id为"+post.getPostId());
    }
    @Test
    void testInsertPostTags(){
        postMapper.insertPostTag(13L,6L);
    }

    @Test
    void testGetPost(){
        System.out.println(postService.getHotspotRange(1L,1));
    }
    @Test
    void testIncrese(){
        postService.incrementReadCount(1L,1L);
    }
}
